package com.fruit.controller.wap.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fruit.constants.RedisConstant;
import com.fruit.controller.base.BaseController;
import com.fruit.pojo.goods.GoodsSpec;
import com.fruit.pojo.goods.GoodsSpecTime;
import com.fruit.pojo.goods.GoodsSpecTimeHarvest;
import com.fruit.resolver.DefiniteException;
import com.fruit.service.goods.GoodsManager;
import com.fruit.util.CommonUtil;
import com.fruit.util.DateUtil;

/**
 * 商品controller
 * 
 * @author shibeilei
 *
 */
@Controller
@RequestMapping(value = "/wap/goods/")
public class GoodsWapController extends BaseController {
	


	@Autowired
	private GoodsManager goodsManager;
	
	/**
	 * 打开商品详情页面
	 */
	@RequestMapping(value = "/detail", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ModelAndView detail(HttpServletRequest request, HttpSession session) {
		String goodsId = request.getParameter("id");
		if (StringUtils.isBlank(goodsId)) {
			throw new DefiniteException("商品id为空");
		}
		ModelAndView view = this.getModelAndView();
		List<GoodsSpecTime> goodsList = goodsManager.getGoodsDetail(Integer.parseInt(goodsId));//详情
		if (goodsList == null || goodsList.size()==0) {
			throw new DefiniteException("商品不存在");
		} else {
				GoodsSpecTime goodsDetail = goodsList.get(0);//详情
				List<GoodsSpecTimeHarvest> gsth = goodsManager.findGoodsSpecTimeHarvest(goodsDetail.getId());
				String[] img = StringUtils.split(goodsDetail.getPicture(),",");
				String imgs = img[0];
				//判断用户是否登录，插入令牌
				Integer userId = (Integer)session.getAttribute("userId");
				if(userId != null){
					String submitToken =  CommonUtil.getUUID();
					goodsManager.setToken(RedisConstant.USER_SUBMIT_TOOKEN+userId, submitToken);
					view.addObject("submitToken", submitToken);
				}
				boolean dateB = DateUtil.compareDate(DateUtil.dTime(goodsDetail.getEndTime()),DateUtil.getDay());
				if(dateB){//true 表示当前系统时间 小于 商品下架时间一样  （商品详情页面的 今日认养时间 为当前系统时间）
					view.addObject("date",DateUtil.getsdfDaymd());
				}else{//false 表示当前系统时间 大于等于 商品下架时间一样  （商品详情页面的 今日认养时间 为下架时间）
					view.addObject("date",DateUtil.dateTime(goodsDetail.getEndTime()));
				}
				view.addObject("goodsDetail", goodsDetail);//商品详情
				view.addObject("img", imgs);//图片
				view.addObject("userId",userId);
				view.addObject("goodsTimeId",goodsId);
				view.addObject("gsth",gsth.get(0));//预计产量
				view.setViewName("good/detail");
		}
		return view;
	}
	
	/**
	 * 打开商品规格列表
	 */
	@RequestMapping(value = "/specList", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public List<GoodsSpec> specList(HttpServletRequest request, HttpSession session) {
		String goodsId = request.getParameter("id");
		Map<String, Object> res =  new HashMap<String, Object>();
		if (StringUtils.isBlank(goodsId)) {
			throw new DefiniteException("商品id为空");
		}
		List<GoodsSpec> goodsList = null;//goodsManager.getGoodsForSpec(Integer.parseInt(goodsId));// 根据规则查询所有商品
		return goodsList;
	}
}
